//
//  QRCode.ScanViewHolder.swift
//  SwiftCode
//
//  Created by yuhus on 2024/1/24.
//

import Foundation
import UIKit
import AVFoundation
import Lottie

extension QRCode {
  class PreviewView: UIView {
    var videoPreviewLayer: AVCaptureVideoPreviewLayer {
      guard let layer = layer as? AVCaptureVideoPreviewLayer else {
        fatalError("Expected `AVCaptureVideoPreviewLayer` type for layer. Check PreviewView.layerClass implementation.")
      }
      return layer
    }
    
    var session: AVCaptureSession? {
      get {
        return videoPreviewLayer.session
      }
      set {
        videoPreviewLayer.session = newValue
      }
    }
    
    override class var layerClass: AnyClass {
      return AVCaptureVideoPreviewLayer.self
    }
  }
  
  final class ScanViewHolder {
    private let scanViewSize = 240.0
    private let scanCornerRadius = 10.0
    
    func inflate(_ parent: UIView) {
      [maskView, scanView, pictureButton].forEach { parent.addSubview($0) }
      maskView.snp.makeConstraints { make in
        make.edges.equalToSuperview()
      }
      scanView.snp.makeConstraints { make in
        make.center.equalToSuperview()
        make.width.height.equalTo(scanViewSize)
      }
      pictureButton.snp.makeConstraints { make in
        make.size.equalTo(54)
        make.centerX.equalToSuperview()
        make.centerY.equalToSuperview().multipliedBy(1.5).offset(scanViewSize / 4)
      }
      
      /// 扫描框
      [scanLine, leftTopView, leftBottomView, rightTopView, rightBottomView].forEach { scanView.addSubview($0) }
      scanLine.snp.makeConstraints { make in
        make.edges.equalToSuperview()
      }
      leftTopView.snp.makeConstraints { make in
        make.leading.equalToSuperview()
        make.top.equalToSuperview()
      }
      leftBottomView.snp.makeConstraints { make in
        make.leading.equalToSuperview()
        make.bottom.equalToSuperview()
      }
      rightTopView.snp.makeConstraints { make in
        make.trailing.equalToSuperview()
        make.top.equalToSuperview()
      }
      rightBottomView.snp.makeConstraints { make in
        make.trailing.equalToSuperview()
        make.bottom.equalToSuperview()
      }
    }
    
    func updateMaskLayer() {
      let layer = CAShapeLayer()
      layer.fillRule = .evenOdd
      let path = UIBezierPath(rect: maskView.bounds)
      
      let cornerRect = scanView.frame
      let roundedPath = UIBezierPath(roundedRect: cornerRect, cornerRadius: scanCornerRadius)
      path.append(roundedPath)
      layer.path = path.cgPath
      maskView.layer.mask = layer
    }
    
    private lazy var maskView: UIView = {
      let view = UIView()
      view.backgroundColor = .black.withAlphaComponent(0.6)
      view.layer.masksToBounds = true
      return view
    }()
    
    lazy var pictureButton: UIButton = {
      let button = UIButton()
      button.setImage(R.image.qrcode_scan_album(), for: .normal)
      return button
    }()
    
    /// 扫描框
    lazy var scanView: UIView = {
      let view = UIView()
      view.layer.cornerRadius = scanCornerRadius
      view.layer.masksToBounds = true
      return view
    }()
    
    /// 扫描线
    private lazy var scanLine: LottieAnimationView = {
      let view = LottieAnimationView(name: "qrcode_scan.json", bundle: R.bundle)
      view.loopMode = .loop
      view.backgroundBehavior = .pauseAndRestore
      view.play()
      return view
    }()
    
    /// 扫描框的四个角
    private lazy var leftTopView = UIImageView(image: R.image.qrcode_scan_corner_lt())
    private lazy var leftBottomView = UIImageView(image: R.image.qrcode_scan_corner_lb())
    private lazy var rightTopView = UIImageView(image: R.image.qrcode_scan_corner_rt())
    private lazy var rightBottomView = UIImageView(image: R.image.qrcode_scan_corner_rb())
  }
}
